package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;

/**
 * 跨域处理
 */
@Configuration
public class GmallCorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter(){

        //cors跨域配置对象
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowCredentials(true);//是否允许携带cookie
        configuration.addAllowedOrigin("http://localhost:1000");//允许域名访问
        configuration.addAllowedMethod("*");//允许什么方式访问  * =>表示允许所有方式访问
        configuration.addAllowedHeader("*");//允许什么头信息访问  *=>表示允许所有头信息访问

        //配置源对象
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**",configuration);
        //cors过滤器对象
        return new CorsWebFilter(configurationSource);
    }
}
